<?php

namespace api\admin;

use utils\zip;

class driver
{
    //添加
    public function add($data){
        $name=$data['name'];
        $phone=$data['phone'];
        $remark=$data['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"insert into driver(name,phone,remark,create_time,update_time) values('$name','$phone','$remark',$time,$time)");
        $driver_id=mysqli_insert_id($con);

        //生成小程序码，拿到链接后更新进对应数据
        $path="pages/menu/driver/info/index";
        $scene="driver_id=$driver_id";
        $url=getWechatQrcode($scene,$path);
        mysqli_query($con,"update driver set url='$url' where id=$driver_id");

        mysqli_close($con);
        if($res){
            return out_right('添加成功！');
        }else{
            return out_error('添加失败！');
        }
    }

    //修改
    public function edit($data){
        $id=(int)$data['id'];
        $time=time();
        $name=$data['name'];
        $phone=$data['phone'];
        $remark=$data['remark'];

        $con=con();
        $res=mysqli_query($con,"update driver set name='$name',phone='$phone',remark='$remark',update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right('修改成功！');
        }else{
            return out_error('修改失败！');
        }
    }

    //删除
    public function del($data){
        $id=(int)$data['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update driver set is_delete=0,delete_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right('删除成功！');
        }else{
            return out_error('删除失败！');
        }
    }

    //获取分页列表
    public function getList($data){
        $name=$data['name'];
        $page=(int)$data['page'];
        $size=(int)$data['size'];
        $start=($page-1)*$size;

        $con=con();
        $where=" where is_delete=1 ";
        if(!empty($name)){
            $where.=" and name like '%$name%'";
        }
        $limit=" order by `id` desc limit $start,$size";
        $sql="select * from driver $where";
        $data=mysqli_query($con,$sql.$limit);
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        for($i=0;$i<count($data);$i++){
            if(empty($data[$i]['url'])){
                $data[$i]['url']=[];
            }else{
                $data[$i]['url']=[$data[$i]['url']];
            }
        }
        mysqli_close($con);
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    //修改状态
    public function changeStatus($request){
        $id=(int)$request['id'];
        $status=(int)$request['status'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update driver set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    //获取二维码压缩包
    public function getZip(){
        $con=con();
        $data=mysqli_query($con,"select * from driver where is_delete=1 and status=1");
        $data=mysqli_fetch_all($data,true);
        mysqli_close($con);
        $res=[];
        for($i=0;$i<count($data);$i++){
            if(!empty($data[$i]['url'])){
                $res[]=[
                    'name'=>$data[$i]['name'],
                    'url'=>$data[$i]['url']
                ];
            }
        }
        $url=zip::get_url($res);
        if(empty($url)){
            return out_error('获取失败！');
        }else{
            return out_right($url);
        }

    }

    //获取全量列表
    public function getAll($data){
        $con=con();
        $data=mysqli_query($con,"select * from driver where is_delete=1 and status=1");
        $data=mysqli_fetch_all($data,true);
        mysqli_close($con);
        return out_right($data);
    }

}